/*     */ package com.zimbra.cs.db;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.mailbox.Metadata;
/*     */ import com.zimbra.cs.mailbox.ScheduledTask;
/*     */ import java.sql.PreparedStatement;
/*     */ import java.sql.ResultSet;
/*     */ import java.sql.SQLException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DbScheduledTask
/*     */ {
/*  41 */   public static String TABLE_SCHEDULED_TASK = "scheduled_task";
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void createTask(DbPool.DbConnection conn, ScheduledTask task)
/*     */     throws ServiceException
/*     */   {
/*  49 */     ZimbraLog.scheduler.debug("Creating %s", new Object[] { task });
/*     */     
/*  51 */     PreparedStatement stmt = null;
/*     */     try {
/*  53 */       stmt = conn.prepareStatement("INSERT INTO " + TABLE_SCHEDULED_TASK + " (class_name, name, mailbox_id, exec_time, interval_millis, metadata) " + "VALUES (?, ?, ?, ?, ?, ?)");
/*     */       
/*     */ 
/*     */ 
/*  57 */       stmt.setString(1, task.getClass().getName());
/*  58 */       stmt.setString(2, task.getName());
/*  59 */       stmt.setInt(3, task.getMailboxId());
/*  60 */       stmt.setTimestamp(4, DbUtil.dateToTimestamp(task.getExecTime()));
/*  61 */       if (task.getIntervalMillis() > 0L) {
/*  62 */         stmt.setLong(5, task.getIntervalMillis());
/*     */       } else {
/*  64 */         stmt.setNull(5, 4);
/*     */       }
/*  66 */       stmt.setString(6, getEncodedMetadata(task));
/*  67 */       stmt.executeUpdate();
/*     */     } catch (SQLException e) {
/*  69 */       throw ServiceException.FAILURE("Unable to create " + task, e);
/*     */     } finally {
/*  71 */       DbPool.closeStatement(stmt);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static List<ScheduledTask> getTasks(String className, int mailboxId)
/*     */     throws ServiceException
/*     */   {
/*  84 */     ZimbraLog.scheduler.debug("Retrieving tasks for class %s, mailbox %d", new Object[] { className, Integer.valueOf(mailboxId) });
/*     */     
/*  86 */     List<ScheduledTask> tasks = new ArrayList();
/*     */     
/*  88 */     DbPool.DbConnection conn = null;
/*  89 */     PreparedStatement stmt = null;
/*  90 */     ResultSet rs = null;
/*     */     try {
/*  92 */       conn = DbPool.getConnection();
/*  93 */       String sql = "SELECT class_name, name, mailbox_id, exec_time, interval_millis, metadata FROM " + TABLE_SCHEDULED_TASK;
/*     */       
/*     */ 
/*  96 */       if (className != null) {
/*  97 */         sql = sql + " WHERE class_name = ?";
/*     */       }
/*  99 */       if (mailboxId > 0) {
/* 100 */         if (className == null) {
/* 101 */           sql = sql + " WHERE mailbox_id = ?";
/*     */         } else {
/* 103 */           sql = sql + " AND mailbox_id = ?";
/*     */         }
/*     */       }
/* 106 */       stmt = conn.prepareStatement(sql);
/* 107 */       int i = 1;
/* 108 */       if (className != null) {
/* 109 */         stmt.setString(i++, className);
/*     */       }
/* 111 */       if (mailboxId > 0) {
/* 112 */         stmt.setInt(i++, mailboxId);
/*     */       }
/*     */       
/* 115 */       rs = stmt.executeQuery();
/* 116 */       while (rs.next()) {
/* 117 */         className = rs.getString("class_name");
/* 118 */         String name = rs.getString("name");
/* 119 */         ScheduledTask task = null;
/*     */         
/*     */         try
/*     */         {
/* 123 */           Object obj = Class.forName(className).newInstance();
/* 124 */           if ((obj instanceof ScheduledTask)) {
/* 125 */             task = (ScheduledTask)obj;
/*     */           } else {
/* 127 */             ZimbraLog.scheduler.warn("Class %s is not an instance of ScheduledTask for task %s", new Object[] { className, name });
/*     */             
/* 129 */             continue;
/*     */           }
/*     */         } catch (Exception e) {
/* 132 */           ZimbraLog.scheduler.warn("Unable to instantiate class %s for task %s.  Class must be an instance of %s and have a constructor with no arguments.", className, name, ScheduledTask.class.getSimpleName(), e);
/*     */         }
/*     */         
/* 135 */         continue;
/*     */         
/*     */ 
/*     */ 
/* 139 */         task.setMailboxId(rs.getInt("mailbox_id"));
/* 140 */         task.setExecTime(DbUtil.timestampToDate(rs.getTimestamp("exec_time")));
/* 141 */         task.setIntervalMillis(rs.getLong("interval_millis"));
/*     */         try
/*     */         {
/* 144 */           setProperties(task, rs.getString("metadata"));
/*     */         } catch (ServiceException e) {
/* 146 */           ZimbraLog.scheduler.warn("Unable to read metadata for %s.  Not scheduling this task.", task, e); }
/* 147 */         continue;
/*     */         
/*     */ 
/* 150 */         tasks.add(task);
/*     */       }
/*     */     } catch (SQLException e) {
/* 153 */       throw ServiceException.FAILURE("Unable to get all DataSourceTasks", e);
/*     */     } finally {
/* 155 */       DbPool.closeResults(rs);
/* 156 */       DbPool.closeStatement(stmt);
/* 157 */       DbPool.quietClose(conn);
/*     */     }
/*     */     
/* 160 */     ZimbraLog.scheduler.info("Loaded %d scheduled data source tasks", new Object[] { Integer.valueOf(tasks.size()) });
/* 161 */     return tasks;
/*     */   }
/*     */   
/*     */   public static void updateTask(DbPool.DbConnection conn, ScheduledTask task) throws ServiceException
/*     */   {
/* 166 */     ZimbraLog.scheduler.debug("Updating %s", new Object[] { task });
/*     */     
/* 168 */     PreparedStatement stmt = null;
/*     */     try {
/* 170 */       stmt = conn.prepareStatement("UPDATE  " + TABLE_SCHEDULED_TASK + " SET mailbox_id = ?, exec_time = ?, interval_millis = ?, metadata = ? " + "WHERE class_name = ? AND name = ?");
/*     */       
/*     */ 
/*     */ 
/* 174 */       stmt.setInt(1, task.getMailboxId());
/* 175 */       stmt.setTimestamp(2, DbUtil.dateToTimestamp(task.getExecTime()));
/* 176 */       if (task.getIntervalMillis() > 0L) {
/* 177 */         stmt.setLong(3, task.getIntervalMillis());
/*     */       } else {
/* 179 */         stmt.setNull(3, 4);
/*     */       }
/* 181 */       stmt.setString(4, getEncodedMetadata(task));
/* 182 */       stmt.setString(5, task.getClass().getName());
/* 183 */       stmt.setString(6, task.getName());
/*     */       
/* 185 */       int numRows = stmt.executeUpdate();
/* 186 */       if (numRows != 1) {
/* 187 */         String msg = String.format("Unexpected number of rows (%d) updated for %s", new Object[] { Integer.valueOf(numRows), task });
/* 188 */         throw ServiceException.FAILURE(msg, null);
/*     */       }
/*     */     } catch (SQLException e) {
/* 191 */       throw ServiceException.FAILURE("Unable to update " + task, e);
/*     */     } finally {
/* 193 */       DbPool.closeStatement(stmt);
/*     */     }
/*     */   }
/*     */   
/*     */   public static void deleteTask(String className, String taskName) throws ServiceException {
/* 198 */     DbPool.DbConnection conn = null;
/*     */     try {
/* 200 */       conn = DbPool.getConnection();
/* 201 */       deleteTask(conn, className, taskName);
/* 202 */       conn.commit();
/*     */     } finally {
/* 204 */       DbPool.quietClose(conn);
/*     */     }
/*     */   }
/*     */   
/*     */   public static void deleteTask(DbPool.DbConnection conn, String className, String taskName)
/*     */     throws ServiceException
/*     */   {
/* 211 */     ZimbraLog.scheduler.debug("Deleting scheduled task from the database.  className=%s, taskName=%s", new Object[] { className, taskName });
/*     */     
/*     */ 
/* 214 */     PreparedStatement stmt = null;
/*     */     try {
/* 216 */       stmt = conn.prepareStatement("DELETE FROM " + TABLE_SCHEDULED_TASK + " WHERE class_name = ? AND name = ?");
/*     */       
/*     */ 
/* 219 */       stmt.setString(1, className);
/* 220 */       stmt.setString(2, taskName);
/* 221 */       stmt.executeUpdate();
/*     */     } catch (SQLException e) {
/* 223 */       throw ServiceException.FAILURE("Unable to delete scheduled task: className=" + className + ", taskName=" + taskName, e);
/*     */     }
/*     */     finally {
/* 226 */       DbPool.closeStatement(stmt);
/*     */     }
/*     */   }
/*     */   
/*     */   private static String getEncodedMetadata(ScheduledTask task) {
/* 231 */     boolean hasProperties = false;
/* 232 */     Metadata metadata = new Metadata();
/* 233 */     Set<String> keys = task.getPropertyNames();
/* 234 */     for (String key : keys) {
/* 235 */       hasProperties = true;
/* 236 */       metadata.put(key, task.getProperty(key));
/*     */     }
/* 238 */     if (!hasProperties) {
/* 239 */       return null;
/*     */     }
/* 241 */     return metadata.toString();
/*     */   }
/*     */   
/*     */   private static void setProperties(ScheduledTask task, String encodedMetadata) throws ServiceException {
/* 245 */     if (StringUtil.isNullOrEmpty(encodedMetadata)) {
/* 246 */       return;
/*     */     }
/*     */     
/* 249 */     Metadata metadata = new Metadata(encodedMetadata);
/* 250 */     Map<String, ?> map = metadata.asMap();
/* 251 */     for (Map.Entry<String, ?> entry : map.entrySet()) {
/* 252 */       if ((entry.getValue() instanceof String)) {
/* 253 */         task.setProperty((String)entry.getKey(), (String)entry.getValue());
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/db/DbScheduledTask.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */